package com.kpx.app.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.kpx.app.R;
import com.kpx.app.base.ActivityBase;
import com.kpx.app.bean.BindCardInfo;
import com.kpx.app.bean.LoginUser;
import com.kpx.app.configs.Constants;
import com.kpx.app.http.HttpDataApi;
import com.kpx.app.http.HttpDataLoader;
import com.kpx.app.http.HttpDataReqUrl;
import com.kpx.app.http.ResObj;
import com.kpx.app.utils.MyHashMap;
import com.kpx.app.utils.UIUtils;
import com.kpx.app.utils.UserInfoUtils;
import com.kpx.app.utils.dbutils.SpUtil;
import com.kpx.app.utils.uiutils.ToastUtil;
import com.kpx.app.view.TitleView;
import com.kpx.app.wigdet.ClearEditText;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/4/3.
 * 添加提现账号
 */

public class ActivityAddTxCard extends ActivityBase {
    @BindView(R.id.titleview)
    TitleView titleview;
    @BindView(R.id.cet_count)
    ClearEditText cetCount;
    @BindView(R.id.txt_1)
    TextView txt1;
    @BindView(R.id.cet_realname)
    ClearEditText cetRealname;
    @BindView(R.id.txt_2)
    TextView txt2;
    @BindView(R.id.cet_phone)
    ClearEditText cetPhone;
    @BindView(R.id.txt_3)
    TextView txt3;
    @BindView(R.id.btn_code)
    Button btnCode;
    @BindView(R.id.cet_code)
    ClearEditText cetCode;
    @BindView(R.id.txt_4)
    TextView txt4;
    @BindView(R.id.btn_save)
    Button btnSave;
    private LoginUser loginUser;

    @Override
    public void initview(@Nullable Bundle savedInstanceState) {
        setContentView(R.layout.activity_add_tx_card);
        ButterKnife.bind(this);
        UIUtils.setNormalTitle(titleview, "提现账号");
        if (!UserInfoUtils.isUserLogin()) {
            UserInfoUtils.showLoginDialog(this, "登录失效，请重新登录", 1);
            return;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (loginUser == null) {
            loginUser = UserInfoUtils.getCachedLoginUser();
        }
        if (loginUser != null && loginUser.getUSER() != null) {
            cetPhone.setText(loginUser.getUSER().getPhone());
            cetPhone.setEnabled(false);
            cetPhone.setClearIconVisible(false);
        }
        long lasttime = SpUtil.getLong(Constants.TIME_CODE_5);
        if (lasttime != 0 && System.currentTimeMillis() - lasttime < 120 * 1000) {
            btnCode.setEnabled(false);
            time = (int) ((120 * 1000 - System.currentTimeMillis() + lasttime) / 1000);
            handler.sendEmptyMessage(0);
        }
    }

    @Override
    public void initData() {

    }

    @Override
    public void onReceiver(Context context, Intent intent) {

    }

    @OnClick({R.id.btn_code, R.id.btn_save})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_code:
                if (TextUtils.isEmpty(cetPhone.getText().toString().trim())) {
                    ToastUtil.getInstance().showToast(ActivityAddTxCard.this, "请输入电话号码");
                    return;
                }
                toGetCode();
                break;
            case R.id.btn_save:
                if (checkedData()) {
                    toSave();
                }
                break;
        }
    }

    private boolean checkedData() {
        if (TextUtils.isEmpty(cetCount.getText().toString().trim())) {
            ToastUtil.getInstance().showToast(ActivityAddTxCard.this, "请输入账号");
            return false;
        }
        if (TextUtils.isEmpty(cetRealname.getText().toString().trim())) {
            ToastUtil.getInstance().showToast(ActivityAddTxCard.this, "请输入姓名");
            return false;
        }
        if (TextUtils.isEmpty(cetCode.getText().toString().trim())) {
            ToastUtil.getInstance().showToast(ActivityAddTxCard.this, "请输入验证码");
            return false;
        }
        return true;
    }

    private int time = 120;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            time--;
            if (time <= 0) {
                btnCode.setEnabled(true);
                btnCode.setText("获取验证码");
            } else {
                btnCode.setText(time + "s");
                handler.sendEmptyMessageDelayed(0, 1000);
            }
        }
    };

    private void toGetCode() {
        showLoadingDialog();
        UserInfoUtils.getCode(ActivityAddTxCard.this, cetPhone.getText().toString().trim(), 4, new UserInfoUtils.OnGetCodeBackListener() {
            @Override
            public void back(boolean s) {
                dissmissLoadingDialog();
                if (s) {
                    time = 120;
                    btnCode.setEnabled(false);
                    handler.sendEmptyMessage(0);
                    ToastUtil.getInstance().showToast(ActivityAddTxCard.this, "获取成功");
                    SpUtil.putLong(Constants.TIME_CODE_5, System.currentTimeMillis());
                }
            }
        });
    }

    /**
     * 参数：token,String accountName(账户名), String realName(真实姓名),String authCode(
     * 验证码)
     */
    private void toSave() {
        showLoadingDialog();
        MyHashMap map = new MyHashMap();
        map.put("accountName", cetCount.getText().toString().trim());
        map.put("realName", cetRealname.getText().toString().trim());
        map.put("authCode", cetCode.getText().toString().trim());
        new HttpDataLoader(new HttpDataApi.OnRequestCallback() {
            @Override
            public void onRequestSuccess(String url, ResObj result, boolean isFrmCache) {
                dissmissLoadingDialog();
                if (result.getCode() == 0) {
//                    BindCardInfo info = new BindCardInfo();
//                    info.setAccountName(cetCount.getText().toString().trim());
//                    info.setRealName(cetRealname.getText().toString().trim());
//                    LoginUser loginUser = UserInfoUtils.getCachedLoginUser();
//                    if (loginUser != null) {
//                        loginUser.setACCOUNT(info);
//                        UserInfoUtils.saveLoginUser(loginUser);
//                    }
                    ToastUtil.getInstance().showToast(ActivityAddTxCard.this, "提现账号添加完成");
                    finish();
                } else {
                    ToastUtil.getInstance().showToast(ActivityAddTxCard.this, result.getMsg());
                }
            }

            @Override
            public void onRequestError(String url, int statusCode, String error) {
                dissmissLoadingDialog();
                ToastUtil.getInstance().showToast(ActivityAddTxCard.this, error);
            }

        }, null, null).POST(HttpDataReqUrl.URL_POST_BIND_ZFB, true, map);
    }

}
